

pipeline {
    agent any

    stages {
        stage('pull code') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs:
                        [[credentialsId: 'ca32ada8-6e93-4c5c-85f6-62924549fa54', url:
                                'https://gitee.com/datau001/junit5_mini.git']]])
            }
        }
        stage('Build, Test, and Generate Allure Report') {
            steps {
                script{
                    // 步骤1: 尝试执行 Maven 清理、测试和生成 Allure 报告
                    try {
                        sh '/Users/gaigai/apache-maven-3.9.0/bin/mvn clean test -Dtest=com.ceshiren.Calculator1Test#sum1,com.ceshiren.CalculatorTest#sum allure:report'
                    } catch (Exception e) {
                        // 步骤1失败时记录错误信息，但继续执行后续步骤
                        currentBuild.result = 'FAILURE'
                        error "步骤1失败: ${e.message}"
                    }
                }
            }
            post {
                always {
                    script {
                        allure includeProperties: false, jdk: '', results: [[path: 'target/allure-results/']]
                    }
                }
            }
        }

        stage('Send CURL POST Requests') {
            steps {
                // 步骤2: 发送第一个 CURL POST 请求并提取令牌
                script {
                    def loginResponse = sh(script: '''

            response=$(curl -X POST -H "Content-Type: application/json; charset=utf-8" -d '{"username": "root01","password": "123456"}' http://127.0.0.1:8088/user/login | /usr/local/bin/jq -r '.data.token')
            url="{\"build_url\": \"$BUILD_URL\"}"
            echo "构建的URL: $url"

            curl -X GET -H "Authorization: Bearer $response" -H "Content-Type: aapplication/json; charset=utf-8" http://127.0.0.1:8088/record/report?build_url=$BUILD_URL

            ''', returnStdout: true)

                }
            }
        }
    }
}

        